/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.persist.util;

import org.apache.commons.lang.CharSet;
import org.apache.commons.lang.StringUtils;

/**
 * 
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">FANGFANG ZHAO</a>
 * @since 0.1.0, Feb 17, 2009
 * @version $Id: StringHelper.java 49 2009-04-24 16:42:55Z zhaofang123@gmail.com $
 */
public class StringHelper {

	private StringHelper() {}
	
	/**
	 * must start with ['a-zA-Z', '_', '#'], can contains ['a-zA-Z', '_', '#', '0-9']
	 * 
	 * @param ident
	 * @return
	 */
	public static boolean isIdentifier(String ident) {
		if (StringUtils.isBlank(ident))
			return false;
		
		char start = ident.charAt(0);
		if ( !(CharSet.ASCII_ALPHA.contains(start) || start == '_' || start == '#') )
			return false;
		
		for (char ch : ident.toCharArray()) {
			if ( !(CharSet.ASCII_ALPHA.contains(ch) || CharSet.ASCII_NUMERIC.contains(ch) || ch == '_' || ch == '#') )
				return false;
		}
		
		return true;
	}
	
	public static String hyphenate(String string) {
		char[] chars = string.toCharArray();
		int lenth = string.length();
		
		StringBuilder sb = new StringBuilder(lenth + 4);
		for (int i = 0; i < lenth; i++) {
			char ch = chars[i];
			if (ch >= 'A' && ch <= 'Z' && i > 0)
				sb.append('_');
			sb.append(ch);
		}
		return sb.toString();
	}
	
	
	public static void main(String[] args) {
		System.out.println(hyphenate("SalesOrderAz894APZy"));
		System.out.println(hyphenate("salesOrderId"));
		System.out.println(hyphenate("AppInstanceSettings"));
		
		System.out.println((int)'A');
		System.out.println((int)'B');
	}
}
